package com.wang.jmonkey.common.handler.file;

import com.wang.jmonkey.common.constant.FileConstant;
import com.wang.jmonkey.common.property.FTPSystemProperty;
import com.wang.jmonkey.common.property.FileSystemProperty;
import com.wang.jmonkey.common.utils.FtpFileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;

@Slf4j
public class FtpSystemHandler implements FileSystemHandlerAdapter {

    @Autowired
    private FileSystemProperty fileSystemProperty;

    @Override
    public boolean uploadFile(String filePath, InputStream file) {

        FTPSystemProperty property = fileSystemProperty.getFtp();

        try {
            return FtpFileUtil.build(property.getIp(), property.getPort(), property.getUser(), property.getPassword())
                    .uploadFile(filePath, file);
        } catch (Exception e) {
            log.error("FtpSystemHandler uploadFile error : ", e);
        }

        return false;
    }

    @Override
    public String getResourcePrefix() {
        return fileSystemProperty.getFtp().getResourcePrefix();
    }

    @Override
    public boolean support(String type) {
        return FileConstant.FileSystemType.FTP.getKey().equalsIgnoreCase(type);
    }
}
